package com.ruoyi.wms.service.impl;

import java.util.List;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.common.constant.UserConstants;
import com.ruoyi.common.utils.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.wms.mapper.WmsSupplierMapper;
import com.ruoyi.wms.domain.WmsSupplier;
import com.ruoyi.wms.service.IWmsSupplierService;

/**
 * 供应商信息Service业务层处理
 * 
 * @author ruoyi
 * @date 2024-01-01
 */
@Service
public class WmsSupplierServiceImpl implements IWmsSupplierService 
{
    @Autowired
    private WmsSupplierMapper wmsSupplierMapper;

    /**
     * 查询供应商信息
     * 
     * @param id 供应商信息主键
     * @return 供应商信息
     */
    @Override
    public WmsSupplier selectWmsSupplierById(Long id)
    {
        return wmsSupplierMapper.selectWmsSupplierById(id);
    }

    /**
     * 查询供应商信息列表
     * 
     * @param wmsSupplier 供应商信息
     * @return 供应商信息
     */
    @Override
    public List<WmsSupplier> selectWmsSupplierList(WmsSupplier wmsSupplier)
    {
        // 设置租户ID
        wmsSupplier.setTenantId(SecurityUtils.getTenantId());
        return wmsSupplierMapper.selectWmsSupplierList(wmsSupplier);
    }

    /**
     * 新增供应商信息
     * 
     * @param wmsSupplier 供应商信息
     * @return 结果
     */
    @Override
    public int insertWmsSupplier(WmsSupplier wmsSupplier)
    {
        wmsSupplier.setTenantId(SecurityUtils.getTenantId());
        wmsSupplier.setCreateBy(SecurityUtils.getUsername());
        wmsSupplier.setCreateTime(DateUtils.getNowDate());
        return wmsSupplierMapper.insertWmsSupplier(wmsSupplier);
    }

    /**
     * 修改供应商信息
     * 
     * @param wmsSupplier 供应商信息
     * @return 结果
     */
    @Override
    public int updateWmsSupplier(WmsSupplier wmsSupplier)
    {
        wmsSupplier.setUpdateBy(SecurityUtils.getUsername());
        wmsSupplier.setUpdateTime(DateUtils.getNowDate());
        return wmsSupplierMapper.updateWmsSupplier(wmsSupplier);
    }

    /**
     * 批量删除供应商信息
     * 
     * @param ids 需要删除的供应商信息主键
     * @return 结果
     */
    @Override
    public int deleteWmsSupplierByIds(Long[] ids)
    {
        return wmsSupplierMapper.deleteWmsSupplierByIds(ids);
    }

    /**
     * 删除供应商信息信息
     * 
     * @param id 供应商信息主键
     * @return 结果
     */
    @Override
    public int deleteWmsSupplierById(Long id)
    {
        return wmsSupplierMapper.deleteWmsSupplierById(id);
    }

    /**
     * 根据供应商编码查询供应商信息
     * 
     * @param supplierCode 供应商编码
     * @return 供应商信息
     */
    @Override
    public WmsSupplier selectWmsSupplierByCode(String supplierCode)
    {
        return wmsSupplierMapper.selectWmsSupplierByCode(supplierCode, SecurityUtils.getTenantId());
    }

    /**
     * 校验供应商编码是否唯一
     * 
     * @param wmsSupplier 供应商信息
     * @return 结果
     */
    @Override
    public boolean checkSupplierCodeUnique(WmsSupplier wmsSupplier)
    {
        Long supplierId = StringUtils.isNull(wmsSupplier.getId()) ? -1L : wmsSupplier.getId();
        WmsSupplier info = wmsSupplierMapper.selectWmsSupplierByCode(wmsSupplier.getSupplierCode(), SecurityUtils.getTenantId());
        if (StringUtils.isNotNull(info) && info.getId().longValue() != supplierId.longValue())
        {
            return UserConstants.NOT_UNIQUE;
        }
        return UserConstants.UNIQUE;
    }
}